home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac Pascal Primer, 4.0 / Chap 5, Zinger ƒ / Zinger.p next >
Text File  |  1990-07-15  |  4KB  |  185 lines

  1. program Zinger;
  2.     const
  3.         BASE_RES_ID = 400;
  4.         SLEEP = 60;
  5.         DRAG_THRESHOLD = 30;
  6.         WNE_TRAP_NUM = $60;
  7.         UNIMPL_TRAP_NUM = $9F;
  8.         POPUP_MENU_ID = BASE_RES_ID;
  9.         NOT_A_NORMAL_MENU = -1;
  10.         POPUP_LEFT = 100;
  11.         POPUP_TOP = 35;
  12.         POPUP_RIGHT = 125;
  13.         POPUP_BOTTOM = 52;
  14.         SHADOW_PIXELS = 1;
  15.         RIGHT_MARGIN = 5;
  16.         BOTTOM_MARGIN = 4;
  17.         LEFT_MARGIN = 5;
  18.         PIXEL_FOR_TOP_LINE = 1;
  19.  
  20.     var
  21.         gDone, gWNEImplemented: BOOLEAN;
  22.         gPopUpItem, gPopUpLabelWidth: INTEGER;
  23.         gPopUpMenu: MenuHandle;
  24.         gTheEvent: EventRecord;
  25.         gPopUpRect, gLabelRect, gDragRect: Rect;
  26.         gPopUpLabelH: StringHandle;
  27.  
  28.  
  29. {-------------------------------->    DrawPopUpNumber    <---}
  30.  
  31.     procedure DrawPopUpNumber;
  32.         var
  33.             menuItem: Str255;
  34.             itemLeftMargin: INTEGER;
  35.     begin
  36.         GetItem(gPopUpMenu, gPopUpItem, menuItem);
  37.         itemLeftMargin := (gPopUpRect.right - gPopUpRect.left - StringWidth(menuItem)) div 2;
  38.         MoveTo(gPopUpRect.left + itemLeftMargin, gPopUpRect.bottom - BOTTOM_MARGIN);
  39.         DrawString(menuItem);
  40.     end;
  41.  
  42.  
  43. {-------------------------------->    DrawPopUp    <---}
  44.  
  45.     procedure DrawPopUp;
  46.     begin
  47.         SetRect(gPopUpRect, POPUP_LEFT, POPUP_TOP, POPUP_RIGHT, POPUP_BOTTOM);
  48.         FrameRect(gPopUpRect);
  49.  
  50.         MoveTo(gPopUpRect.left + SHADOW_PIXELS, gPopUpRect.bottom);
  51.         LineTo(gPopUpRect.right, gPopUpRect.bottom);
  52.         LineTo(gPopUpRect.right, gPopUpRect.top + SHADOW_PIXELS);
  53.  
  54.         MoveTo(gPopUpRect.left - gPopUpLabelWidth - RIGHT_MARGIN, gPopUpRect.bottom - BOTTOM_MARGIN);
  55.         HLock(Handle(gPopUpLabelH));
  56.         DrawString(gPopUpLabelH^^);
  57.         HUnlock(Handle(gPopUpLabelH));
  58.  
  59.         gLabelRect.top := gPopUpRect.top + PIXEL_FOR_TOP_LINE;
  60.         gLabelRect.left := gPopUpRect.left - gPopUpLabelWidth - LEFT_MARGIN - RIGHT_MARGIN;
  61.         gLabelRect.right := gPopUpRect.left;
  62.         gLabelRect.bottom := gPopUpRect.bottom;
  63.  
  64.         DrawPopUpNumber;
  65.     end;
  66.  
  67.  
  68. {-------------------------------->    HandleMouseDown    <---}
  69.  
  70.     procedure HandleMouseDown;
  71.         var
  72.             whichWindow: WindowPtr;
  73.             thePart, i: INTEGER;
  74.             theChoice: LONGINT;
  75.             myPoint, popUpUpperLeft: Point;
  76.     begin
  77.         thePart := FindWindow(gTheEvent.where, whichWindow);
  78.         case thePart of
  79.             inContent: 
  80.                 begin
  81.                     myPoint := gTheEvent.where;
  82.                     GlobalToLocal(myPoint);
  83.                     if PtInRect(myPoint, gPopUpRect) then
  84.                         begin
  85.                             InvertRect(gLabelRect);
  86.                             popUpUpperLeft.v := gPopUpRect.top + PIXEL_FOR_TOP_LINE;
  87.                             popUpUpperLeft.h := gPopUpRect.left;
  88.                             LocalToGlobal(popUpUpperLeft);
  89.                             theChoice := PopUpMenuSelect(gPopUpMenu, popUpUpperLeft.v, popUpUpperLeft.h, gPopUpItem);
  90.                             InvertRect(gLabelRect);
  91.                             if LoWord(theChoice) > 0 then
  92.                                 begin
  93.                                     gPopUpItem := LoWord(theChoice);
  94.                                     DrawPopUpNumber;
  95.                                     for i := 0 to gPopUpItem - 1 do
  96.                                         SysBeep(20);
  97.                                 end;
  98.                         end;
  99.                 end;
  100.             inSysWindow: 
  101.                 SystemClick(gTheEvent, whichWindow);
  102.             inDrag: 
  103.                 DragWindow(whichWindow, gTheEvent.where, screenBits.bounds);
  104.             inGoAway: 
  105.                 gDone := TRUE;
  106.         end;
  107.     end;
  108.  
  109.  
  110. {-------------------------------->    HandleEvent    <---}
  111.  
  112.     procedure HandleEvent;
  113.         var
  114.             dummy: BOOLEAN;
  115.     begin
  116.         if gWNEImplemented then
  117.             dummy := WaitNextEvent(everyEvent, gTheEvent, SLEEP, nil)
  118.         else
  119.             begin
  120.                 SystemTask;
  121.                 dummy := GetNextEvent(everyEvent, gTheEvent);
  122.             end;
  123.  
  124.         case gTheEvent.what of
  125.             mouseDown: 
  126.                 HandleMouseDown;
  127.             updateEvt: 
  128.                 begin
  129.                     BeginUpdate(WindowPtr(gTheEvent.message));
  130.                     DrawPopUp;
  131.                     EndUpdate(WindowPtr(gTheEvent.message));
  132.                 end;
  133.         end;
  134.     end;
  135.  
  136.  
  137. {-------------------------------->    MainLoop    <---}
  138.  
  139.     procedure MainLoop;
  140.     begin
  141.         gDone := FALSE;
  142.         gWNEImplemented := (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) <> NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  143.         while gDone = FALSE do
  144.             HandleEvent;
  145.     end;
  146.  
  147.  
  148. {-------------------------------->    MenuBarInit    <---}
  149.  
  150.     procedure MenuBarInit;
  151.     begin
  152.         gPopUpMenu := GetMenu(POPUP_MENU_ID);
  153.         InsertMenu(gPopUpMenu, NOT_A_NORMAL_MENU);
  154.         gPopUpLabelH := GetString(BASE_RES_ID);
  155.         HLock(Handle(gPopUpLabelH));
  156.         gPopUpLabelWidth := StringWidth(gPopUpLabelH^^);
  157.         HUnlock(Handle(gPopUpLabelH));
  158.         gPopUpItem := 1;
  159.     end;
  160.  
  161.  
  162. {-------------------------------->    WindowInit    <---}
  163.  
  164.     procedure WindowInit;
  165.         var
  166.             popUpWindow: WindowPtr;
  167.     begin
  168.         popUpWindow := GetNewWindow(BASE_RES_ID, nil, WindowPtr(-1));
  169.         SetPort(popUpWindow);
  170.         ShowWindow(popUpWindow);
  171.  
  172.         TextFont(systemFont);
  173.         TextMode(srcCopy);
  174.     end;
  175.  
  176.  
  177. {-------------------------------->    Zinger    <---}
  178.  
  179. begin
  180.     WindowInit;
  181.     MenuBarInit;
  182.     DrawPopUp;
  183.  
  184.     MainLoop;
  185. end.